<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 楼栋信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
            v-model="query.region"
            placeholder="校区名"
            class="handle-input mr10"
        ></el-input>
        <el-input
            v-model="query.name"
            placeholder="公寓名"
            class="handle-input mr10"
        ></el-input>
        <!--        <el-input-->
        <!--          v-model="query.type"-->
        <!--          placeholder="类型"-->
        <!--          class="handle-input mr10"-->
        <!--        ></el-input>-->
        <!--        <el-form-item label="类型" prop="type">-->
        <!--          <div>-->
        <el-radio v-model="query.type" label="男生公寓" border> 男生公寓</el-radio>
        <el-radio v-model="query.type" label="女生公寓" border>女生公寓</el-radio>
        <!--          </div>-->
        <!--        </el-form-item>-->
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
        >搜索
        </el-button
        >
        <el-button
            type="primary"
            icon="el-icon-circle-plus-outline"
            @click="handleAdd"
        >新增
        </el-button
        >
      </div>
      <div class="clits">
        <div><i class="el-icon-s-data"></i>楼栋信息列表</div>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          :show-overflow-tooltip="true"
          :prop="item.prop"
          :label="item.label"
          v-for="item in props"
          :key="item.prop"
           align="center"
          sortable 
        >
        </el-table-column >
        <!-- <el-table-column >
        </el-table-column> -->
        <el-table-column>
          <el-table-column
            label="操作"
            width="180"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.row.sid)"
                >编辑
              </el-button>
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.$index, scope.row.sid)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :currentPage="query.pageNum"
          :page-sizes="[10, 20, 40]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageTotal"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import request from "../../utils/request";

export default {
  name: "Building",
  setup() {
    const root = "/building/";
    const router = useRouter();

    const props = [
      {label: "公寓名称", prop: "name"},
      {label: "所属校区", prop: "region"},
      {label: "公寓类型", prop: "type"},
      {label: "所属楼层", prop: "floor"},
      {label: "房间数量", prop: "room"},
    ];

    const query = reactive({
      region: "",
      name: "",
      type: "",
      pageNum: 1,
      pageSize: 10,
    });
    const tableData = ref([]);
    const pageTotal = ref(0);
    // 获取表格数据
    const getData = () => {
      request
        .get(root + "page", {
          params: query,
        })
        .then((res) => {
          if (res.code == 18) {
            ElMessage.warning(res.msg);
            tableData.value = [];
          } else {
            tableData.value = res.data.records;
            for (var i = 0; i < tableData.value.length; i++) {
              if (tableData.value[i].room == null) {
                tableData.value[i].room = "--";
              }
            }
            pageTotal.value = res.data.total;
          }
        });
    };
    getData();

    // 查询操作
    const handleSearch = () => {
      query.pageNum = 1;
      getData();
    };
    // 分页大小
    const handleSizeChange = (val) => {
      query.pageSize = val;
      getData();
    };
    // 分页导航
    const handlePageChange = (val) => {
      query.pageNum = val;
      getData();
    };

    // 删除操作
    const handleDelete = (index, sid) => {
      // 二次确认删除
      ElMessageBox.confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          request.delete(root + sid).then((res) => {
            if (res.data) {
              ElMessage.success("删除成功");
              tableData.value.splice(index, 1);
            } else {
              ElMessage.error("删除失败");
            }
          });
        })
        .catch(() => {});
    };

    // 新增操作
    const handleAdd = () => {
      router.push("/admin/addBuilding");
    };

    // 表格编辑时弹窗和保存
    const editVisible = ref(false);
    // let form = reactive({
    //   name: "",
    //   address: "",
    // });
    const handleEdit = (sid) => {
      router.push({ path: "/admin/addBuilding", query: { id: sid } });
    };

    return {
      props,
      query,
      tableData,
      pageTotal,
      editVisible,
      //   form,
      handleSearch,
      handleSizeChange,
      handlePageChange,
      handleAdd,
      handleDelete,
      handleEdit,
    };
  },
};
</script>

<style scoped>
.clits {
  background: #3ca2e0;
  height: 50px;
  font-size: 25px;
  display: flex;
  align-items: center;
}
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
